//
//  ST_FirstChargeController.swift
//  btvideo
//
//  Created by mac on 2021/4/29.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import MBProgressHUD

class ST_FirstChargeController: UIViewController {

    deinit {
        SCComponets.alert_dispatcher?.waiting_status.accept(false)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        SCComponets.alert_dispatcher?.waiting_status.accept(true)
        
        view.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            if ScreenHeight <= 667 {
                make.top.equalToSuperview()
            }else {
                make.top.equalTo(55 + statusBarHeight)
            }
        }
        
        view.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.height.equalTo(TIANCompatibleValue(value: 429))
            make.left.right.equalToSuperview()
            make.top.equalTo(backView).offset(26)
        }
        
        view.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.top.equalTo(cover_image_view).offset(18)
            make.right.equalToSuperview().offset(-10)
            make.size.equalTo(CGSize(width: 42, height: 42))
        }
        
        cover_image_view.addSubview(countdown_view)
        countdown_view.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(TIANCompatibleValue(value: 118))
            make.height.equalTo(24)
            make.width.equalTo(202)
            make.centerX.equalToSuperview()
        }
        
//        countdown_view.addSubview(alarmImageView)
//        alarmImageView.snp.makeConstraints { (make) in
//            make.left.equalToSuperview().offset(35)
//            make.centerY.equalToSuperview()
//        }
        
        countdown_view.addSubview(count_down_label)
        count_down_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        view.addSubviews([tipLabel_1, tipLabel_2, tipLabel_3])
        tipLabel_1.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(48)
            make.top.equalTo(cover_image_view.snp.bottom).offset(21)
        }
        
        tipLabel_2.snp.makeConstraints { make in
            make.left.equalTo(tipLabel_1)
            make.top.equalTo(tipLabel_1.snp.bottom).offset(8)
        }
        
        tipLabel_3.snp.makeConstraints { make in
            make.left.equalTo(tipLabel_1)
            make.top.equalTo(tipLabel_2.snp.bottom).offset(8)
        }
        
        view.addSubview(charge_btn)
        charge_btn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel_3.snp.bottom).offset(29)
            make.width.equalTo(190)
            make.height.equalTo(42)
            make.centerX.equalToSuperview()
        }
        
    }
    

// MARK: - Properties
    var productId:String?
    
    fileprivate lazy var backView: fistChargeBackView = {
        let v = fistChargeBackView()
        return v
    }()
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
//        if let url = Bundle.main.url(forResource: "redCharge", withExtension: "webp"){
//            if let data = try? Data(contentsOf: url){
//                control.image = UIImage.sd_image(withWebPData: data)
//            }
//        }
        control.image = ST_ImageUtils.imageName("first_back")
        return control
    }()
    
    fileprivate lazy var close_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImage(ST_ImageUtils.imageName("first_close"), for: UIControl.State.normal)
        control.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        control.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        return control
    }()
    
    fileprivate lazy var countdown_view:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xD84948)
//        control.borderColor = UIColor.init(hex: 0xFDB6C2)
//        control.borderWidth = 1
        control.cornerRadius = 12
        return control
    }()
    
    fileprivate lazy var count_down_label:UILabel = {
        let control = UILabel.init()
        control.textColor = .white
//        control.textColor = UIColor(hex: 0xEC4A63)
        control.font = UIFont.systemFont(ofSize: 13, weight: UIFont.Weight.medium)
        return control
    }()

    fileprivate lazy var charge_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = UIColor.init(hex: 0xDD736D)
        control.setTitleForAllStates("立即领取")
        control.setTitleColorForAllStates(.white)
        control.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
//        control.backgroundColor = UIColor.init(white: 0.5, alpha: 0.5)
        control.cornerRadius = 21
        control.addTarget(self, action: #selector(payHandle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
//    fileprivate lazy var alarmImageView:UIImageView = {
//        let control = UIImageView(image: ST_ImageUtils.imageName("first_charge_alarm"))
//        return control
//    }()
    
    fileprivate lazy var tipLabel_1: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.init(hex: 0x232323)
        let att = "首充豪礼1:".withAttributes([.font(UIFont.systemFont(ofSize: 14, weight: .medium))])
        att.append(" 价值15元，66个甜心币".withAttributes([.font(UIFont.systemFont(ofSize: 14, weight: .regular))]))
        l.attributedText = att
        return l
    }()
    
    fileprivate lazy var tipLabel_2: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.init(hex: 0x232323)
        let att = "首充豪礼2:".withAttributes([.font(UIFont.systemFont(ofSize: 14, weight: .medium))])
        att.append(" 价值10元，免费语音视频通话1次".withAttributes([.font(UIFont.systemFont(ofSize: 14, weight: .regular))]))
        l.attributedText = att
        return l
    }()
    
    fileprivate lazy var tipLabel_3: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.init(hex: 0x232323)
        let att = "首充豪礼3:".withAttributes([.font(UIFont.systemFont(ofSize: 14, weight: .medium))])
        att.append(" 额外喜欢次数10次".withAttributes([.font(UIFont.systemFont(ofSize: 14, weight: .regular))]))
        l.attributedText = att
        return l
    }()
}

extension ST_FirstChargeController{
    
    
    @objc fileprivate func cancel_handle(){
        
        goback_previous_controller(animated: false)
        
    }
    
    func timer_handle(str:String?){
        
        if let timeStr = str {
            count_down_label.text = "限时领取倒计时：" + timeStr
        }else{
            cancel_handle()
        }
        
    }
    
    @objc fileprivate func payHandle(){
    
//        //TIANPayManager.shared.source = self.isVideo ? .语音冲币 :  .视频冲币
        guard let id = productId else { return  }
        
        ST_MaidWayManager.shared.source = .首充
        
        ST_MaidWayManager.shared.doWay(id: id, delegate: self)
        
    }
    

}

extension ST_FirstChargeController:ST_TIANPaymentTransactionStateDelegate{
    
     func transaction_succeed(){
         
        self.toast(text: "领取成功")
        
        main_thread_execute(afterDelay: 3) {
            self.goback_previous_controller()
        }
        
     }
    
}

class fistChargeBackView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if ScreenHeight > 667 {
            self.roundCorners([UIRectCorner.topLeft,UIRectCorner.topRight], radius: 30)
        }
    }
}
